//
//  MJTHomeTableViewController.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/9.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SVProgressHUD

//重用标记
private let HomeTableViewCellIndentifier = "HomeTableViewCellIndentifier"
class MJTHomeTableViewController: MJTVisitorTableViewController {
    //懒加载ViewModel对象
    private lazy var statusListViewModel: MJTStatusListViewModel = MJTStatusListViewModel()
    // 懒加载上拉刷新控件
    private lazy var pullUpView: UIActivityIndicatorView = {
        let indecatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indecatorView.color = UIColor.redColor()
        return indecatorView
    }()
    // 使用系统的下拉刷新
//    private lazy var pullDownView: UIRefreshControl = {
//        let ctr = UIRefreshControl()
//        //添加下拉刷新事件
//        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
//        return ctr
//    }()
    //自定义下拉刷新
    private lazy var pullDownView: MJTRefreshControl = {
        let ctr = MJTRefreshControl()
        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        return ctr
    }()
    //懒加载tip 动画label
    private lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 12)
        label.backgroundColor = UIColor.redColor()
        label.textAlignment = .Center
        //默认隐藏
        label.hidden = true
        return label
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        if isLogin {
            setupUI()
            setupTableView()
            //登录加载数据
            loadStatus()
        } else {
            vistorView?.updateVisitorViewInfo(nil, imageName: nil)
        }
    }
    private func setupUI() {
        //tipLabel添加到导航栏控制器视图的底部
        guard let nav = self.navigationController else {
            return
        }
        // 把tipLabel添加到导航栏的底部
        nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
        // 设置Frame
        tipLabel.frame = CGRect(x: 0, y: 64-30, width: nav.navigationBar.width, height: 30)
    }
    //开启动画
    private func startAnimation(message: String) {
        //如果正在执行动画,则不让其执行动画
        if tipLabel.hidden == false {
            return
        }
        tipLabel.text = message
        tipLabel.hidden = false
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.height)
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                     self.tipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        self.tipLabel.hidden = true
                })
        }
    }
    //  设置tableview相关操作
    private func setupTableView() {
        //注册cell
        tableView.registerClass(MJTStatusTableViewCell.self, forCellReuseIdentifier: HomeTableViewCellIndentifier)

        //去掉分割线
        tableView.separatorStyle = .None
        //设置自动行高,通过约束设置高度
        tableView.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView.estimatedRowHeight = 200
        //集成系统的下拉刷新,两种方式
        //第一种直接设置UITableViewController里的refreshControl的属性
//        self.refreshControl = pullDownView
        //第二种方式
//        tableView.addSubview(pullDownView)

        //添加自定义下拉刷新
        tableView.addSubview(pullDownView)

        //添加footView
        tableView.tableFooterView = pullUpView
        //指定大小
        pullUpView.sizeToFit()

    }
    //下拉刷新
    @objc private func pullDownRefresh() {
        loadStatus()
       // self.pullDownView.endRefreshing()

    }
    //结束刷新
    private func endRefreshing() {
        //停止动画
        pullUpView.stopAnimating()
        //结束系统的下拉刷新
      //self.pullDownView.endRefreshing()
        pullDownView.endRefreshing()

    }
    //加载微博数据
    private func loadStatus() {

//        SVProgressHUD.show()
        statusListViewModel.loadStatus(pullUpView.isAnimating()) { (isSuccess,message) -> () in
//            SVProgressHUD.dismiss()
            if !self.pullUpView.isAnimating() {
                //开启动画
                self.startAnimation(message)
            }
            self.endRefreshing()
            if isSuccess {
                self.tableView.reloadData()
            }
        }

    }
}
extension MJTHomeTableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeTableViewCellIndentifier, forIndexPath: indexPath) as! MJTStatusTableViewCell
        //设置选择样式
        cell.selectionStyle = .None

        let statusViewModel = statusListViewModel.statusList[indexPath.row]
        cell.statusViewModel = statusViewModel
        return cell
    }
    // 将要显示那个cell
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //判断是否是最后一个cell
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullUpView.isAnimating() {
            //开启菊花转\
            pullUpView.startAnimating()
            //数据请求
            loadStatus()

        }
    }

}

